﻿#include "GameCore.h"

GameCore::_GameButtonValue GameCore::ComputerChuPai(int player, int nStep, int nCanChoiceCount, _GameChuPaiType eType, int nValue, int nParam1, int nParam2, QVariant nParam3)
{
    /*
    if(nType == GameCore::Game_ChuPai_Type_ZhuDong)
    {
        if(step)
        if(nCanChoiceCount > 0)
        {
            m_arrayPlayer[player].comIndex;
        }
    }*/


    //首先:可以点确定
    if(m_flagsButton[Game_Button_OK] ==  _BaseObject::Flag_Enable)
    {
        _ClickButton_(Game_Button_OK);
        return Game_Button_OK;
    }

    //选择的指针
    int i = 0;
    int *p_i = &i;      //被动和弃牌时为临时i,(不能更改point)
    if(eType == Game_ChuPai_Type_ZhuDong)
        p_i = &(m_arrayPlayer[m_nGameRound].m_nChuPaiPoint);


    //开始按顺序检测出牌
    //如果可选的牌比最少需要的多
    if(nCanChoiceCount > 0 && nCanChoiceCount >= m_arrayStep[nStep].nMinCount)
    {
        switch(eType)
        {
        case Game_ChuPai_Type_ZhuDong:
        case Game_ChuPai_Type_Pai:
        case Game_ChuPai_Type_JiNeng:
            {
                //第0步
                if(nStep == 0)
                {
                    if(nValue == _Pai::Value_HanBing)   //寒冰,电脑点取消
                        break;
                    else if(nValue == _Pai::Value_Tao && nParam3 != player)   //桃,电脑只救自己
                        break;

                    //循环找牌
                    for(; *p_i < m_arrayPlayer[player].m_listShouPai.count(); (*p_i) ++)
                    {
                        _Pai* p = m_arrayPlayer[player].getShouPaiData(*p_i);
                        if(p == nullptr)
                        {
                            emit s_Critical(_T("[!GameCore]ComputerChuPai ERROR!"));
                            return Game_Button_ERROR;
                        }

                        if((p->flags() & _BaseObject::Flag_Enable)
                            && !(p->flags() & _BaseObject::Flag_Clicked))
                        {
                            _ClickPai_(p);

                            //m_DianJiPai.pObject->setSomeFlags(_BaseObject::Flag_Clicked);
                            return Game_Button_OK;
                        }
                    }
                }
                //第N步
                else if(nStep > 0)
                {
                    //switch()
                    //如果是拆和顺
                    if(nStep == 2
                            && (
                                m_arrayPlayer[player].getShouPaiData(m_arrayStep[0].nSelectValue[0])->value() == _Pai::Value_Chai
                                || m_arrayPlayer[player].getShouPaiData(m_arrayStep[0].nSelectValue[0])->value() == _Pai::Value_Shun
                                )
                            )
                    {
                        //选武器
                        if(m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getPaiCount(_BaseObject::Area_ZhuangBei,_BaseObject::Area_ZhuangBei) > 0)
                        {
                            _Pai *p = m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getZhuangBeiData(_BaseObject::Area_ZhuangBei);
                            if(p == nullptr)
                            {
                                emit s_Critical(_T("[!GameCore]ComputerChuPai ERROR 2!"));
                                return Game_Button_ERROR;
                            }

                            _ClickPai_(p);

                            //p->setSomeFlags(_BaseObject::Flag_Clicked);
                            return Game_Button_OK;
                        }
                        //选手牌
                        else if(m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getPaiCount(_Pai::Value_All, _BaseObject::Area_ShouPai) > 0)
                        {
                            _Pai *p = m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getShouPaiData((int)Random(0,m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].m_listShouPai.count()));
                            if(p == nullptr)
                            {
                                emit s_Critical(_T("[!GameCore]ComputerChuPai ERROR 3!"));
                                return Game_Button_ERROR;
                            }

                            _ClickPai_(p);

                            //p->setSomeFlags(_BaseObject::Flag_Clicked);
                            return Game_Button_OK;
                        }
                        //选判定
                        else if(m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getPaiCount(_Pai::Value_All, _BaseObject::Area_PanDing) > 0)
                        {
                            _Pai *p = m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].getPandingData((int)Random(0,m_arrayPlayer[m_arrayStep[1].nSelectValue[0]].m_listPanDing.count()));

                            _ClickPai_(p);

                            //p->setSomeFlags(_BaseObject::Flag_Clicked);
                            return Game_Button_OK;
                        }
                        else
                        {
                            emit s_Critical(_T("[!GameCore]电脑选牌出错!但游戏可以继续 WARNING!"));
                            break;
                        }
                    }

                    if(m_arrayStep[nStep].nType & _BaseObject::Area_ShouPai)
                    {
                        int i;
                        for(i = 0; i < m_arrayPlayer[player].m_listShouPai.count(); i++)
                        {
                            _Pai* p = m_arrayPlayer[player].getShouPaiData(i);
                            if(p == nullptr)
                            {
                                emit s_Critical(_T("[!GameCore]ComputerChuPai ERROR 4!"));
                                return Game_Button_ERROR;
                            }
                            if((p->flags() & _BaseObject::Flag_Enable)
                                    && !(p->flags() & _BaseObject::Flag_Clicked))
                            {
                                _ClickPai_(p);

                                //m_DianJiPai.pObject->setSomeFlags(_BaseObject::Flag_Clicked);
                                return Game_Button_OK;
                            }
                        }
                    }
                    if(m_arrayStep[nStep].nType & _BaseObject::Area_Player)
                    {
                        //!!!???
                        //电脑打它的下一家
                        /*
                        int i = GetNextPlayer(player);
                        for(; i != player; i = GetNextPlayer(i))
                        {
                        }*/


                        //  电脑随机打
                        int t = (int)Random(0, m_nPlayerCount);
                        int i = t;
                        do
                        {
                            if((m_arrayPlayer[i].flags() & _BaseObject::Flag_Enable)
                                    && !(m_arrayPlayer[i].flags() & _BaseObject::Flag_Clicked))
                            {
                                _ClickPlayer_(i);

                                //m_DianJiPai.pObject->setSomeFlags(_BaseObject::Flag_Clicked);
                                return Game_Button_OK;
                            }
                        }while(i = GetNextPlayer(i), i != t);


                        //  电脑按顺序打
                        /*int i;
                        for(i = 0; i < playerCount; i++)
                        {
                        }*/
                    }
                }
            }
            break;

        case Game_ChuPai_Type_QiPai:
            break;

        default:
            emit s_Critical(_T("[!GameCore]ComputerChuPai ERROR!"));
            setGameStatus(Game_Status_ERROR);
            return Game_Button_ERROR;
        }
    }



    //取消可以点
    if(m_flagsButton[Game_Button_Cancel] ==  _BaseObject::Flag_Enable)
    {
        (*p_i)++;       //如果一张牌的步骤最后没可点时,则点击取消后执行下一张
        //if((*p_i) <= m_arrayPlayer[m_nGameRound].m_listShouPai.length())
        {
            _ClickButton_(Game_Button_Cancel);

            //emit s_Critical(_T("此牌不能出，却能点？step:%1,value:%2,").arg(nStep).arg(m_arrayPlayer[player].getShouPaiData(*p_i)->value()));

            return Game_Button_Cancel;
        }
    }

    //弃牌可以点
    if(m_flagsButton[Game_Button_Giveup] ==  _BaseObject::Flag_Enable)
    {
        _ClickButton_(Game_Button_Giveup);
        return Game_Button_Giveup;
    }

    if(m_arrayPlayer[m_nGameRound].m_nChuPaiPoint >
            m_arrayPlayer[m_nGameRound].m_listShouPai.length())
    {
        emit s_Critical(_T("[!GameCore]电脑 ERROR!"));
        setGameStatus(Game_Status_ERROR);
        return Game_Button_ERROR;
    }
    emit s_Critical(_T("[!GameCore]电脑什么都不能点 ERROR!"));
    setGameStatus(Game_Status_ERROR);
    return Game_Button_ERROR;
}

int GameCore::ComputerQiPai(int player, int min, int max)
{
    qint32 arr[GAME_MAX_NUM_SLOT_SHOUPAI];
    if(!GetDifferentNumber(0, m_arrayPlayer[player].m_listShouPai.count(), arr, max, sizeof(qint32)))
    {
        emit s_Critical(_T("[!GameCore]随机范围 ERROR!"));
        setGameStatus(Game_Status_ERROR);
        return -1;
    }
    memset(m_arrayStep,0,sizeof(m_arrayStep));
    //qDebug()<<"随机弃牌"<<max<<m_arrayPlayer[player].m_listShouPai.count();
    for(; max > 0; max--)
    {
        m_arrayStep[0].nSelectValue[m_arrayStep[0].nSelectCount] = arr[max - 1];
        m_arrayStep[0].nSelectType[m_arrayStep[0].nSelectCount] = m_arrayPlayer[player].getShouPaiData(max - 1)->positionArea();
        m_arrayStep[0].nSelectPlayerID = player;        //牌 所属玩家ID
        m_arrayStep[0].nSelectCount++;
        //qDebug()<<"随机弃牌"<<max<<arr[max - 1];
    }
    return 0;
}
